package com.jjty.dataGenerator.data.controller;

import com.jjty.dataGenerator.common.ApiResult;
import com.jjty.dataGenerator.data.service.DataReportService;
import com.jjty.dataGenerator.data.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/report/xzn")
public class XZNDataReportController {
    @Autowired
    private DataReportService dataReportService;
    @Autowired
    private OperationLogService operationLogService;

    @RequestMapping("/reportCallRecordData")
    public ApiResult reportCallRecordData(int numRecords){
        try{
            dataReportService.reportCallRecordData(numRecords);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.doFailure(e.getMessage());
        }
        return ApiResult.doSuccess("上报成功");
    }

    @RequestMapping("/reportXykRecordData")
    public ApiResult reportXykRecordData(int numRecords){
        try{
            dataReportService.reportXykRecordData(numRecords);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.doFailure(e.getMessage());
        }
        return ApiResult.doSuccess("上报成功");
    }

    @RequestMapping("/reportAttendanceRecordData")
    public ApiResult reportAttendanceRecordData(int numRecords){
        try{
            dataReportService.reportAttendanceRecordData(numRecords);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.doFailure(e.getMessage());
        }
        return ApiResult.doSuccess("上报成功");
    }

    @RequestMapping("/reportTemperatureRecordData")
    public ApiResult reportTemperatureRecordData(int numRecords){
        try{
            dataReportService.reportTemperatureRecordData(numRecords);
        }catch (Exception e){
            e.printStackTrace();
            return ApiResult.doFailure(e.getMessage());
        }
        return ApiResult.doSuccess("上报成功");
    }

    @RequestMapping("/reportData")
    public ApiResult reportData(String dataType, String url, HttpServletRequest request) {
        String operationIp = request.getRemoteAddr();
        String operationType = "reportData";
        String operationStatus;
        try {
            dataReportService.reportData(dataType, url);
            operationStatus = "成功";
        } catch (Exception e) {
            operationStatus = "失败";
            e.printStackTrace();
        }
        operationLogService.saveLog(operationType, operationIp, operationStatus,dataType);
        return ApiResult.doSuccess("上报成功");
    }
}
